<?php
/**
* @package Joomla.Tutorials
* @subpackage Components
* @link http://dev.joomla.org/component/option,com_jdwiki/
Itemid,31/id,tutorials:modules/
* @license GNU/GPL
*/
// no direct access
defined( '_JEXEC' ) or die( 'Restricted access' );
jimport('joomla.application.component.controller');
/**
* Hello World Component Controller
*
* @package Joomla.Tutorials
* @subpackage Components
*/
class AccessrulesController extends JController
{

	/**
	* constructor (registers additional tasks to methods)
	* @return void
	*/
	
	function __construct() 	{
		parent::__construct();
		// Register Extra tasks
		$this->registerTask( 'add' , 'edit' );
	}
	
	/**
	* Method to display the view
	*
	* @access public
	*/
	function display() {
		parent::display();
	}
	
	
	/**
	* display the edit form
	* @return void
	*/
	function edit()	{
		JRequest::setVar( 'view', 'hr' );//lay thu muc: administrator\components\com_hello\views\hello\ (giong cau thuc thu muc cua ...\views\hellos\
		JRequest::setVar( 'layout', 'form' ); //"form": lay trong thu muc views\hellos\tmpl\form.php
		JRequest::setVar('hidemainmenu', 1);
		parent::display();
	}
	
	
	/**
	* display the edit form
	* @return void
	*/
	function editUser_Group()	{
		JRequest::setVar( 'view', 'user_group' );
		//JRequest::setVar( 'layout', 'form' );
		//JRequest::setVar('hidemainmenu', 1);
		parent::display();
	}
	
	/**
	* save a record (and redirect to main page)
	* @return void
	*/
	function saveGroup()	{
		$model = $this->getModel('Accessrules');
	
		$strValueGroupName = JRequest::getVar( 'valueGroupName', "", 'post', 'string' );
		$intGroupId = JRequest::getVar( 'groupIdName', -1, 'post', 'int' );
		$msg = $model->saveGroup($intGroupId, $strValueGroupName);
		
		$this->setRedirect( 'index.php?option=com_accessrule&task=editUser_Group', $msg);
	}
	
	/**
	* Get all of Groups
	*/
	function getGroups () {
		global $mainframe;
		$model = $this->getModel('Accessrules');
		$__data = $model->getGroups();
		
		$i = 0;
		if (!empty($__data)) {
			foreach ($__data as $row) {
				echo '<input type="radio" name="groupRadio" id="idGroupRadio'.$i.'" onclick ="" value="'.$row->id.'" />'.$row->name.'&nbsp;&nbsp;&nbsp;'.
					 '(<a href = "javascript:editGroup(\''.$row->name.'\', \''.$row->id.'\');">'.JText::_("EDIT").'</a>) &nbsp;'.
					 '(<a href = "javascript:deleteGroup(\''.$row->name.'\', \''.$row->id.'\');">'.JText::_("DELETE").'</a>)&nbsp;'.
					 '(<a href = "javascript:document.getElementById(\'idGroupRadio'.$i.'\').checked = true; getUsersByGroup(\''.$row->id.'\', \''.$row->name.'\');">'.JText::_("VIEW_USER").'</a>)'.
					 '<br/>';
				$i++;
			}
		}
		echo $return."&nbsp;";
		$mainframe->close();
	}
	
	/**
	* Delete User from Group
	*/
	function deleteUserFromGroup() {
		global $mainframe;
		$model = $this->getModel('Accessrules');
		$msg = $model->deleteUserFromGroup(JRequest::getVar( 'idUser', -1, 'get', 'int' ));		
		echo $return.$msg."&nbsp;";
		$mainframe->close();
	}
	
	/**
	* Add User into Group
	*/
	function addUserIntoGroup () {
		global $mainframe;
		$model = $this->getModel('Accessrules');
		
		$idUser = JRequest::getVar( 'idUser', -1, 'get', 'int' );
		$groupId = JRequest::getVar( 'groupId', -1, 'get', 'int' );
		$groupName = JRequest::getVar( 'groupName', -1, 'get', 'string' );
		
		$msg = $model->addUserIntoGroup($idUser, $groupId, $groupName);		
		echo $return.$msg."&nbsp;";
		$mainframe->close();
	}
	
	/** Get Users By Group
	 * @status: TRUE means get all of users being in Group, otherwise FALSE
	 */	
	function getUsersByGroup() {
		global $mainframe;
		$model = $this->getModel('Accessrules');
		
		$intGroupId = JRequest::getVar( 'groupIdName', -1, 'get', 'int' );
		$status= JRequest::getVar( 'status', "", 'get', 'string' );
		
		//TRUE means All of Users being in Group
		if (!strcmp (strtolower($status), strtolower('TRUE'))) {
			$__data = $model->getUsersByGroup($intGroupId, true);		
			$i = 0;
			if (!empty($__data)) {
				foreach ($__data as $row) {
					echo '<input type="checkbox" name="usersByGroupCheckbox" id="idusersByGroupCheckbox'.$i.'" onclick ="" value="'.$row->id.'" />'.$row->name.'&nbsp;&nbsp;&nbsp;'.
						 '(<a href = "javascript:document.getElementById(\'groupId\').value = '.$intGroupId.';deleteUserFromGroup(\''.$row->id.'\', \''.$intGroupId.'\');">'.JText::_("DELETE").'</a>)'.
						 '<br/>';
					$i++;
				}
			}
		}

		//FALSE means All of Users NOT being in Group
		if (!strcmp (strtolower($status), strtolower('FALSE'))) {
			$__data = $model->getUsersByGroup($intGroupId, false);		
			$i = 0;
			if (!empty($__data)) {
				foreach ($__data as $row) {
					echo '<input type="checkbox" name="usersByGroupCheckbox" id="idusersByGroupCheckbox'.$i.'" onclick ="" value="'.$row->id.'" />'.$row->name.'&nbsp;&nbsp;&nbsp;'.
						 '(<a href = "javascript:addUserIntoGroup(\''.$row->id.'\');">'.JText::_("ADD_INTO_GROUP").'</a>)'.
						 '<br/>';
					$i++;
				}
			}
		}
		echo $return."&nbsp;";
		$mainframe->close();
	}
	
	
	/**
	* Delete Groups
	*/
	function deleteGroup () {
		$model = $this->getModel('Accessrules');
	
		$intGroupId = JRequest::getVar( 'groupIdName', -1, 'post', 'int' );
		$msg = $model->deleteGroup($intGroupId);		
		$this->setRedirect( 'index.php?option=com_accessrule&task=editUser_Group', $msg);
	}
	
	/**
	* save a record (and redirect to main page)
	* @return void
	*/
	//function searchContents()	{
		/*$model = $this->getModel('hello');
		if ($model->store()) {
			$msg = JText::_( 'Greeting Saved!' );
		} else {
			$msg = JText::_( 'Error Saving Greeting' );
		}
		// Check the table in so it can be edited.... we are done with it	anyway*/
		//JRequest::setVar( 'view', 'accessrules' );
		//parent::display();
		//JRequest::setVar( 'layout', 'form' )
		
		//$link = 'index.php?option=com_accessrule';
		//$this->setRedirect($link, "TUYEN");
	//}
	
	/**
	* remove record(s)
	* @return void
	*/
	function remove() {
		
	}
	
	/**
	* cancel editing a record
	* @return void
	*/
	function cancel() {
		$msg = JText::_( 'Operation Cancelled' );
		$this->setRedirect( 'index.php?option=com_hello', $msg );
	}
	
	
	
	function searchUserType() {  
		global $mainframe;
		$model = $this->getModel('Accessrules');
		
		$userTypeName= JRequest::getVar( 'userTypeName', -1, 'get', 'string' );
		
		//echo (JRequest::getVar( 'userTypeName', -1, 'post', 'string' ));
		
		if (!strcmp (strtolower($userTypeName), strtolower('User'))) {
			$__data = $model->getUsers();
		} else if (!strcmp (strtolower($userTypeName), strtolower('Group'))) {
			$__data = $model->getGroups();
		} else {
			$return = JText::_("CHOICE_USER_PLEASE");
		}
		
		$i = 0;
		if (!empty($__data)) {
			foreach ($__data as $row) {
				echo '<input type="checkbox" name="userTypeCheckbox" id="idUserTypeCheckbox'.$i.'" onclick ="" value="'.$row->id.'" />'.$row->name.'<br/>';
				$i++;
			}
		}
		echo $return."&nbsp;";
		$mainframe->close();
	}
	
	function searchContents() {  
		global $mainframe;
		$model = $this->getModel('Accessrules');
		
		//$userTypeName= JRequest::getVar( 'userTypeName', -1, 'get', 'string' );
		
		$typeofSerachContent= JRequest::getVar( 'typeofSerachContent', -1, 'get', 'string' );
		
		$__idSections  = JRequest::getVar( 'idSections', -1, 'get', 'int' );
		$__idCategories = JRequest::getVar( 'idCategories', -1, 'get', 'int' );

		if (!strcmp (strtolower($typeofSerachContent), strtolower('CONTENT'))) {
			$__data = $model->getContentsBySecAndCat ($__idSections, $__idCategories);
		} else if (!strcmp (strtolower($typeofSerachContent), strtolower('CATEGORY'))) {
			$__data = $model->getCategories ($__idSections);
		}
		$i = 0;
	    $return = "";
		if (!empty($__data)) {
			foreach ($__data as $row) {
				$return =  $return.'<input type="checkbox" name="contentCheckbox" id="idcontentCheckbox'.$i.'" onclick ="" value="'.$row->id.'" />'.'&nbsp;';
				$return = $return.$row->title;
				$return = $return.'<br/>';
				$i++;
			}
		}
		echo $return.'&nbsp;';//prevent blank case
		$mainframe->close();
	}
	
	function saveAll() {  
		global $mainframe;
		$model = $this->getModel('Accessrules');

		$__request_arrUserTypeCheckbox = JRequest::getVar( 'arrUserTypeCheckbox', array(0), 'get', 'array' );//Display from Request "1,2,3,4"
		$arrUserTypeCheckbox = explode(",", $__request_arrUserTypeCheckbox[0] );//Remove "," and insert elements into array
		
		$__request_arrContentsCheckbox = JRequest::getVar( 'arrContentsCheckbox', array(0), 'get', 'array' );//Display from Request "1,2,3,4"
		$arrContentsCheckbox = explode(",", $__request_arrContentsCheckbox[0] );//Remove "," and insert elements into array
		
//		$userTypeName= JRequest::getVar( 'userTypeName', -1, 'get', 'string' );
		
		$return = $model->saveAll ($arrUserTypeCheckbox, $arrContentsCheckbox, JRequest::getVar( 'userTypeName', -1, 'get', 'string' ), JRequest::getVar( 'contentTypeName', -1, 'get', 'string' ), JRequest::getVar( 'rules', -1, 'get', 'string' ));
		echo $return.'&nbsp;';      
		$mainframe->close();
	}
}
?>